package com.wy2cloud.esdemo.controller;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.wy2cloud.esdemo.entity.SystemUser;
import com.wy2cloud.esdemo.service.UserService;
import io.searchbox.core.BulkResult;
import io.searchbox.core.DocumentResult;
import io.swagger.annotations.ApiOperation;

@RestController
public class HomeController {
    private final static Logger logger=LoggerFactory.getLogger(HomeController.class);
    
    @Autowired
    private UserService userService;

    
    @GetMapping({"/",""})
    public String index() {
        logger.info("在{}，访问了应用首页", System.currentTimeMillis());
        logger.error("在{}，访问应用首页时异常", System.currentTimeMillis());
        return "success";
    }

	
	@ApiOperation(value="添加信息", notes="添加信息")
	@PostMapping({"/add"})
	public String add(@RequestBody SystemUser user) throws Exception{
	    DocumentResult result = userService.insertDocument("indextest", "typetest", user);
	    logger.info("添加的用户信息: {}", user);
		return result.getJsonString();
	}
	@ApiOperation(value="添加信息", notes="添加信息")
    @PostMapping({"/adds"})
    public String add(@RequestBody List<SystemUser> users) throws Exception{
	    BulkResult result = userService.insertDocument("indextest", "typetest", users);
        return result.getJsonString();
    }
	
	@PostMapping("/createIndex")
    public String createIndex(@RequestParam(name="indexName") String indexName) throws Exception {
        return userService.createIndex(indexName).getJsonString();
    }
	
	@PostMapping("/deleteIndex")
    public String deleteIndex(@RequestParam(name="indexName") String indexName) throws Exception {
        return userService.deleteIndex(indexName).getJsonString();
    }
	
	@GetMapping("/nodesInfo")
	public String nodesInfo() throws Exception {
		return userService.nodesInfo().getJsonString();
	}
	@GetMapping("/nodesStats")
	public String nodesStats() throws Exception {
		return userService.nodesStats().getJsonString();
	}
}
